package com.lidroid.util;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.idroid.R;


/**
 * 统一titlebar
 *
 */
public class TitleHolder {
    private ImageView mIvLeft;
    private TextView mTvLeft;
    private ImageView mIvRight1;
    private ImageView mIvRight2;
    private ImageView mIvArrow;
    private TextView mTvTitle;
    private TextView mTvSubTitle;
    private TextView mTvRight;
    private TextView mTvRightNum;
    private RelativeLayout mRlTitleBar;

    public TitleHolder(View view) {
        mIvLeft = (ImageView) view.findViewById(R.id.left_image);
        mTvLeft=(TextView)view.findViewById(R.id.left_text);
        mIvRight1 = (ImageView) view.findViewById(R.id.right_image1);
        mIvRight2 = (ImageView) view.findViewById(R.id.right_image2);
        mIvArrow = (ImageView) view.findViewById(R.id.iv_arrow);
        mTvTitle = (TextView) view.findViewById(R.id.title_text);
        mTvSubTitle = (TextView) view.findViewById(R.id.title_sub_text);
        mTvRight = (TextView) view.findViewById(R.id.right_text);
        mTvRightNum =(TextView)view.findViewById(R.id.tv_nouse_num);
        mRlTitleBar = (RelativeLayout) view.findViewById(R.id.titlebar);
    }

    public void defineTitle(String title) {
        mTvTitle.setText(title);
    }
    public void defineRight(String title) {
        mTvRight.setText(title);
    }

    public void defineSubTitle(String title) {
        mTvSubTitle.setText(title);
    }

    public void setTitleBarBg(int color){
        mRlTitleBar.setBackgroundResource(color);
    }

    public void setTitleColor(int color){
        mTvTitle.setTextColor(color);
    }

    public void setRightColor(int color){
        mTvRight.setTextColor(color);
    }

    public void defineTitle(String title, int resId) {
        mTvTitle.setText(title);
        mRlTitleBar.setBackgroundResource(resId);
    }

    public void defineTitle(String title, View.OnClickListener l) {
        mTvTitle.setText(title);
        mTvTitle.setOnClickListener(l);
    }
    public void defineTitle(String title, View.OnClickListener l,Drawable rightDrawable) {
        mTvTitle.setText(title);
        mTvTitle.setOnClickListener(l);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        mTvTitle.setCompoundDrawables(null,null,rightDrawable,null);
    }
    public void setLeftVisiblity(int flag){
        mIvLeft.setVisibility(flag);
    }

    public void defineLeft(int resId, View.OnClickListener l) {
        mIvLeft.setVisibility(View.VISIBLE);
        mIvLeft.setImageResource(resId);
        mIvLeft.setOnClickListener(l);
    }

    public void defineLeft(View.OnClickListener l) {
        mIvLeft.setVisibility(View.VISIBLE);
        mIvLeft.setImageResource(R.drawable.back);
        mIvLeft.setOnClickListener(l);
    }

    public void defineRight1(int resId, View.OnClickListener l) {
        mIvRight1.setVisibility(View.VISIBLE);
        mIvRight1.setImageResource(resId);
        mIvRight1.setOnClickListener(l);
    }

    public void defineRight2(int resId, View.OnClickListener l) {
        mIvRight2.setVisibility(View.VISIBLE);
        mIvRight2.setImageResource(resId);
        mIvRight2.setOnClickListener(l);
    }
    //消息显示用
    public void defineRightNum(int resId,int resNum ,View.OnClickListener l){
        mIvRight1.setVisibility(View.VISIBLE);
        mIvRight1.setImageResource(resId);
        if (resNum!=0){
            mTvRightNum.setVisibility(View.VISIBLE);
            mTvRightNum.setText(""+resNum);
        }else{
            mTvRightNum.setVisibility(View.INVISIBLE);
        }
        mIvRight1.setOnClickListener(l);
    }

    public void defineArrow(int resId) {
        mIvArrow.setVisibility(View.VISIBLE);
        mIvArrow.setImageResource(resId);
    }

    public View getRightView(){
        if(mIvRight1.getVisibility()==View.VISIBLE)
            return mIvRight1;
        else if(mIvRight2.getVisibility()==View.VISIBLE)
            return mIvRight2;
        else if(mTvRight.getVisibility()==View.VISIBLE)
            return mTvRight;
        return null;
    }

    public View getTitleView(){
        return mTvTitle;
    }

    public void setRightTextSize(int size){
        mTvRight.setTextSize(size);
    }

    public View getRightViewOperation(){
        return mTvRight;
    }

    public void defineRight(String text, View.OnClickListener l) {
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText(text);
        mTvRight.setOnClickListener(l);
    }
    public void defineLeft(String text, View.OnClickListener l) {
        mIvLeft.setVisibility(View.GONE);
        mTvLeft.setVisibility(View.VISIBLE);
        mTvLeft.setText(text);
        mTvLeft.setOnClickListener(l);
    }
}
